/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gae;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ValidationAware;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings("serial")
public class AuthorizationInterceptor extends AbstractInterceptor{

	private static final String LOGIN = "googleLogin";
	
	@Override
    public String intercept(ActionInvocation invocation) throws Exception {
        
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        if (user != null) {

            return invocation.invoke();


        } else {
        	
            return LOGIN;
        }
       
    }

    private void addActionError(ActionInvocation invocation, String message) {
        Object action = invocation.getAction();
        if (action instanceof ValidationAware) {
            ((ValidationAware) action).addActionError(message);
        }
    }

}
